package com.pan.cuiniubi.read;

import com.pan.cuiniubi.entity.Member;
import com.pan.cuiniubi.entity.Table;
import com.pan.cuiniubi.utils.TypeTool;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ReadSql {

    public void saveTableName(Table table ,String line){
        line = line.trim();
        String tableName = Arrays.stream(line.split("^create\\s+table\\s+|\\(")).reduce((x,y)->x+y).get();
        tableName = tableName.substring(0,1).toUpperCase()+tableName.substring(1);
        table.setTableName(tableName.trim());
    }

    public Boolean isTableStartLine(String line){
        line = line.trim();
        Boolean result = line.matches("^create\\s+table\\s+.*\\(");
        return result;
    }

    /**
     * 这里如果是表格结束返回 false
     * 否则返回true
     * @param line
     * @return
     */
    public Boolean isTableEndLine(String line){
        Boolean result = line.matches("\\s*\\)\\s*;\\s*");
        return !result;
    }

    public Member readLine(String line){
        Member member = new Member();
        String str[] = line.split("\\s+");
        str = Arrays.stream(str).map(String::trim).filter(s -> s.length()>0).toArray(String[]::new);
        member.setMemberName(str[0]);
        member.setMemberType(TypeTool.changeType(str[1]));
        Boolean isPrimary = Arrays.stream(str).skip(2).anyMatch(s->s.equals("primary"));
        member.setIsPrimary(isPrimary);
        String surplus = Optional.ofNullable(Arrays.stream(str).skip(2).reduce((x, y)->x+" "+y)).get().orElse("");
        member.setSurplus(surplus);
        return member;
    }


    /**
     * 读取每一
     * @param path
     * @return
     * @throws IOException
     */
    public List<Table> readFile(String path) throws IOException {

        Stream<String> lineStream = Files.lines(Paths.get(path));

        List<String> lines = lineStream.map(String::trim).filter(line -> line.length()!=0)
                .map(line->line.split("--")[0]).collect(Collectors.toList());

        Boolean isTable = false;
        List<Table> tableList = new ArrayList<>();
        Table table = new Table();
        List<Member> memberList=new ArrayList<>();
        for (String line:lines){
            if (!isTable){
                isTable = isTableStartLine(line);
                if (isTable){
                    table = new Table();
                    memberList = new ArrayList<>();
                    saveTableName(table,line);
                    continue;
                }
            }
            if (isTable){
                isTable = isTableEndLine(line);
                if (!isTable) {
                    table.setMemberList(memberList);
                    tableList.add(table);
                    continue;
                }
                Member member = readLine(line);
                memberList.add(member);
            }

        }
        return tableList;
    }
    /**
     * 读取input文件夹，获得要转化的sql文件路径
     * @return
     */
    public List<String> readDir(){
        String myRootPath = System.getProperty("user.dir");
        String sqlDirPath = myRootPath + "\\input";
        File sqlDirFile = new File(sqlDirPath);
        File[] sqlFiles = sqlDirFile.listFiles();
        List<String> allNames = Arrays.stream(sqlFiles).map(file -> sqlDirPath+"\\"+file.getName()).collect(Collectors.toList());
        return allNames;
    }

    /**
     * 调用这个方法获取所有sql文件的表
     * @return
     * @throws IOException
     */
    public List<Table> getTable() throws IOException {
        List<Table> tableList = new ArrayList<>();

        List<String> paths = readDir();
        for(String path: paths){
            List<Table> tables = readFile(path);
            tableList.addAll(tables);
        }
        return tableList;
    }
}
